package com.example.orderfoodsystem22201310liuziming.controller;

import com.example.orderfoodsystem22201310liuziming.repository.MenuRepository;

import com.example.orderfoodsystem22201310liuziming.utils.ThumbnailUtis;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;

@Controller
@RequestMapping("/photo")
public class PhotoController {
    @Autowired //接线
    private MenuRepository menuRepository;

    @Value("${dishphoto.url}")
    private String dishUrl;

    @Value("${dishphoto.dir}")
    private String dishDir;

    @PostMapping("/upload")  //卖家上传图片复制到指定位置
    public String uplaod( @RequestParam("file") MultipartFile file,@RequestParam("id") String id) throws IOException {//1. 接受上传的文件 @RequestParam("file") MultipartFile file
        String destFileName = dishDir + id + ".jpg";
        //第一次运行的时候，这个文件所在的目录往往是不存在的，这里需要创建一下目录
        File destFile = new File(destFileName);
        destFile.getParentFile().mkdirs();
        //把浏览器上传的文件复制到希望的位置
        file.transferTo(destFile);
        //图片压缩
        ThumbnailUtis.imageZoom(destFileName,666,1f,destFileName);
        return "redirect:/seller/menulist2";
    }


}